// pages/favour_details/favour_details.js
var utils = require('../../utils/util.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pid:'',
    data:[],
    isPraise:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    var pid = options.id
    _this.data.pid = pid
    _this.getPhotoDetails(pid)
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {
        var res = res
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 获取照片详细信息
   */
  getPhotoDetails: function (id) {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Photo&a=getPhotoDetails",
      data: {
        id: id
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          _this.setData({
            data:res.data.data,
            isPraise:res.data.data.is_praise
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  doPraise: function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Photo&a=doPraise",
      data: {
        id: _this.data.pid
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '点赞成功',
            duration: 2000,
            mask: true,
          })
          _this.getPhotoDetails(_this.data.pid)
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var _this = this
    app.checkUserSessionKey()
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '嘿，朋友快来帮我点赞吧！',
      path: '/pages/favour_detail/favour_detail?id='+_this.data.pid,
      success: function (res) {
        // 转发成功
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log('转发成功');
          wx.getShareInfo({
            shareTicket: res.shareTickets,
            success: function (res) {
              var res = res
              _this.doShare(res.encryptedData, res.iv)
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          wx.showModal({
            title: '转发失败',
            content: res.errMsg,
            showCancel: false
          })
        }
      },
      fail: function (res) {
        // 转发失败else 
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          console.log('转发取消');
          wx.showToast({
            title: '转发取消',
            duration: 1000,
            mask: true,
          })
        } else {
          wx.showModal({
            title: '转发失败',
            content: res.errMsg,
            showCancel: false
          })
        }
      }
    }
  },

  /**
   * 分享
   */
  doShare: function (encryptedData, iv) {
    var _this = this
    var pid = _this.data.pid
    console.log(encryptedData)
    console.log(iv)
    wx.request({
      url: app.globalData.requestip + "&c=Share&a=doPhotoShare",
      data: {
        encryptedData: encryptedData,
        iv: iv,
        pid: pid
      },
      header: app.globalData.header,
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          _this.getPhotoDetails(pid)
          wx.showToast({
            title: '分享成功'
          })
        } else if (res.data.code == 4001) {
          wx.showToast({
            title: '已分享至该群'
          })
        } else if (res.data.code == 4002) {
          wx.showToast({
            title: '超出今日次数'
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },
})